/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package nuce.bo;

import dataaccess.DataAccess;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import nuce.entity.User;

/**
 *
 * @author LINHNC
 */
public class UserBO {

    public int insert(User user) {
        final String procName = "User_Insert";
        try {
            dataaccess.DataAccess da = new DataAccess();
            if (da != null) {
                String[] args = new String[2];
                args[0] = user.getName();
                args[1] = user.getPass();
                da.executeProc(procName, args);
                return 1;
            }
            return 0;
        } catch (Exception e) {
            return 0;
        }
    }

    public boolean update(User user) {
        final String procName = "User_update";
        try {
            dataaccess.DataAccess da = new DataAccess();
            if (da != null) {
                String[] args = new String[3];
                args[0] = String.valueOf(user.getId());
                args[1] = user.getName();
                args[2] = user.getPass();
                da.executeProc(procName, args);
                return true;
            }
            return false;
        } catch (Exception e) {
            return false;
        }
    }

    public boolean Delete(User user) {
        final String procName = "User_Delete";
        try {
            dataaccess.DataAccess da = new DataAccess();
            if (da != null) {
                final String[] args = new String[1];
                args[0] = Integer.toString(user.getId());
                da.executeProc(procName, args);
                return true;
            }
            return false;
        } catch (Exception e) {
            return false;
        }
    }

    public User selectById(int id) {
        final String procName = "User_SelectById";
        final String colId = "Id";
        final String colName = "Name";
        final String colPass = "Pass";
        DataAccess da = new DataAccess();
        String[] args = new String[1];
        args[0] = String.valueOf(id);
        ResultSet rs = da.executeProc(procName, args);
        if (rs != null) {
            try {
                rs.next();
                User usr = new User();
                usr.setId(Integer.parseInt(rs.getString(colId)));
                usr.setName(rs.getString(colName));
                usr.setPass(rs.getString(colPass));
                return usr;
            } catch (SQLException ex) {
                Logger.getLogger(User.class.getName()).log(Level.SEVERE, null, ex);
                return null;
            }
        }

        return null;
    }
    public User selectUser(String name, String pass){
        final String procName = "User_SelectUser";
        final String colId = "Id";
        final String colName = "Name";
        final String colPass = "Pass";
        DataAccess da = new  DataAccess();
        String [] args = new String[2];
        args[0] = name; 
        args[1] = pass;
        ResultSet rs = da.executeProc(procName, args);
        if (rs!=null){
            try {
                rs.next();
                User usr = new User();
                usr.setId(Integer.parseInt(rs.getString(colId)));
                usr.setName(rs.getString(colName));
                usr.setPass(rs.getString(colPass));
                return usr;
            } catch (Exception e) {
                return null;
            }
        }
        return null;
    }
}
